/*
 * Copyright (C) 2011 Project R'yleh Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.Arkham;

import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.View.OnClickListener;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class main extends Activity implements OnClickListener
{
	private static final String TAG = main.class.getSimpleName();
	
    /** Called when the activity is first created. */
	public static Timer timer;
	 private ViewCore GLView;
	 static boolean savedGame = false;
	 Actor testingChar;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
    	timer = new Timer();
    	
        super.onCreate(savedInstanceState);
      
        
        //removes title bar and sets the theme
        this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
        
        //sets program to full screen
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
        
       //initiates camera and sets camera 
        GLView = new ViewCore(this); //instantiates the view
        GLView.setTimer(timer);
        setContentView(GLView); //sets it
        
        LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        
        //grabs mainmenu.xml to use as display layout
        ViewGroup ViewMain = (ViewGroup) inflater.inflate(com.Arkham.R.layout.mainmenu, null);
        
        this.addContentView(ViewMain, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

        TextView tv = (TextView) findViewById(com.Arkham.R.id.gameTitle);
        
        GLView.renderer.setmTextView(tv);
        loadScene();

          //Set up the click listeners for all buttons
        View newGameButton = findViewById(com.Arkham.R.id.newgame_button);
        newGameButton.setOnClickListener(this);
        View continueButton = findViewById(com.Arkham.R.id.continue_button);
        continueButton.setOnClickListener(this);
        View exitButton = findViewById(com.Arkham.R.id.exit_button);
        exitButton.setOnClickListener(this); 
        View creditsButton = findViewById(com.Arkham.R.id.credits_button);
        creditsButton.setOnClickListener(this);
        View tutorialButton = findViewById(com.Arkham.R.id.btnTutorial);
        tutorialButton.setOnClickListener(this);
        
//        View targetButton = findViewById(com.Arkham.R.id.targeting_button);
//        targetButton.setOnClickListener(this);
//        View demoButton = findViewById(com.Arkham.R.id.demo_button);
//        demoButton.setOnClickListener(this); 
//        View debugButton = findViewById(com.Arkham.R.id.DebugButton);
//        debugButton.setOnClickListener(this);
//        View practiceButton = findViewById(com.Arkham.R.id.practice_button);
//        practiceButton.setOnClickListener(this);
    }
    
    public void onClick(View v) {
    	Intent i;
    	switch (v.getId())
    	{
    	case com.Arkham.R.id.newgame_button:
	    	i = new Intent(this, NewGame01.class);
	    	startActivity(i);
	    	GLView.renderer.clearScene();
	    	break;
    	case com.Arkham.R.id.continue_button:
    		//if(savedGame){
    			i = new Intent(this, GameMenu.class);
    			GameFile game = new GameFile();
    	    	try 
    	    	{
    				GameFile.getGameFile();
    			} catch (IOException e) 
    			{e.printStackTrace();}
    			
    	    	//GLView.renderer.clearScene();
    	    	startActivity(i);
    		//}
    		//else{
    		//	Toast.makeText(this, "No saved game file found", Toast.LENGTH_SHORT).show();
    		//}
	    	break;
//    	case com.Arkham.R.id.practice_button:
//	    	i = new Intent(this, Practice.class);
//	    	GLView.renderer.clearScene();
//	    	startActivity(i);
//	    	break;
//    	case com.Arkham.R.id.targeting_button:
//    		//setting up character data for demo purposes
//    		GameFile.party.NewGame();
//    		createDemoCharacter();
//    		GLView.renderer.clearScene();
//			///////////////////////////
//			//Set up the intro battle//
//			///////////////////////////
//			BattleInfo.resetParties();
//			//create and add battle map
//			boolean[][] bMap = new boolean[15][15];
//			for(int w=0; w<15; w++)
//				for(int h=0; h<15; h++)
//					bMap[w][h] = false;
//			BattleInfo.setBattleMap(bMap);
//			//Add main character to battle
//			BattleInfo.addGoodCharacterToParty(GameFile.party.characters[0], 2, 7);
//			//create three enemies with sprites
//			Character B = new Character();
//			Character M1 = new Character();
//			Character M2 = new Character();
//			B.setSpriteId(com.Arkham.R.drawable.mage);
//			M1.setSpriteId(com.Arkham.R.drawable.ninja1);
//			M2.setSpriteId(com.Arkham.R.drawable.ninja2);
//			Actor BS = new Actor(2,2, BitmapFactory.decodeResource(getResources(), com.Arkham.R.drawable.mage), 128,128);
//			Actor M1S = new Actor(1,1, BitmapFactory.decodeResource(getResources(), com.Arkham.R.drawable.ninja1), 64,64);
//			Actor M2S = new Actor(1,1, BitmapFactory.decodeResource(getResources(), com.Arkham.R.drawable.ninja2), 64,64);
//			B.RandomizeMonster(2, "Matt", BS);
//			M1.RandomizeMonster(1, "Minion1, Purple Ninja", M1S);
//			M2.RandomizeMonster(1, "Minion2, Red Ninja", M2S);
//			B.equipItem(Armory.Pencil(), Constants.R_HAND);
//			M1.equipItem(Armory.Pencil(), Constants.R_HAND);
//			M2.equipItem(Armory.Pencil(), Constants.R_HAND);
//			B.equipItem(Armory.UsedEraser(), Constants.L_HAND);
//			M1.equipItem(Armory.UsedEraser(), Constants.L_HAND);
//			M2.equipItem(Armory.UsedEraser(), Constants.L_HAND);
//			B.equipItem(Armory.StreetCloths(), Constants.BODY);
//			B.equipItem(Armory.DunceCap(), Constants.HEAD);
//			//Add three enemies to battle
//			BattleInfo.addBadCharacterToParty(M1, 11, 10);
//			BattleInfo.addBadCharacterToParty(M2, 11, 4);
//			BattleInfo.addBadCharacterToParty(B, 13, 7);
//			BattleInfo.nextToAct = BattleInfo.Good.get(0);
//			/////////////////////////
//			//finished battle setup//
//			/////////////////////////
//	    	i = new Intent(this, Targeting.class);
//	    	GLView.renderer.clearScene();
//	    	startActivity(i);
//	    	break;
    	case com.Arkham.R.id.exit_button:
    		GLView.renderer.clearScene();
    	    finish();
    	    break;
//    	case com.Arkham.R.id.demo_button:
//    		//set up the new game information in party
//    		GameFile.party.NewGame();
//    		for(int j=0; j<GameFile.numStoryFlags; j++)
//    			GameFile.storyFlag[j] = false;
//    		//go straight to battle option
//    		GameFile.storyFlag[0] = true;
//    		GameFile.storyFlag[1] = true;
//    		//setting up character data for demo purposes
//    		createDemoCharacter();
//    		GameFile.party.characters[0].gainALevel();
//    		GLView.renderer.clearScene();
//	    	i = new Intent(this, GameMenu.class);
//		    startActivity(i);
//		    break;
//    	case com.Arkham.R.id.DebugButton:
//    		//setting up character data for demo purposes
//    		GameFile.party.NewGame();
//    		createDemoCharacter();
//    		GLView.renderer.clearScene();
//			///////////////////////////
//			//Set up the intro battle//
//			///////////////////////////
//			BattleInfo.resetParties();
//			//create and add battle map
//			boolean[][] battleMap = new boolean[15][15];
//			for(int w=0; w<15; w++)
//				for(int h=0; h<15; h++)
//					battleMap[w][h] = false;
//			BattleInfo.setBattleMap(battleMap);
//			//Add main character to battle
//			BattleInfo.addGoodCharacterToParty(GameFile.party.characters[0], 2, 7);
//			//create three enemies with sprites
//			Character Boss = new Character();
//			Character Minion1 = new Character();
//			Character Minion2 = new Character();
//			Boss.setSpriteId(com.Arkham.R.drawable.mage);
//			Minion1.setSpriteId(com.Arkham.R.drawable.ninja1);
//			Minion2.setSpriteId(com.Arkham.R.drawable.ninja2);
//			Actor BossSprite = new Actor(2,2, BitmapFactory.decodeResource(getResources(), com.Arkham.R.drawable.mage), 128,128);
//			Actor M1Sprite = new Actor(1,1, BitmapFactory.decodeResource(getResources(), com.Arkham.R.drawable.ninja1), 64,64);
//			Actor M2Sprite = new Actor(1,1, BitmapFactory.decodeResource(getResources(), com.Arkham.R.drawable.ninja2), 64,64);
//			Boss.RandomizeMonster(2, "Matt", BossSprite);
//			Minion1.RandomizeMonster(1, "Minion1, Purple Ninja", M1Sprite);
//			Minion2.RandomizeMonster(1, "Minion2, Red Ninja", M2Sprite);
//			Boss.equipItem(Armory.Pencil(), Constants.R_HAND);
//			Minion1.equipItem(Armory.Pencil(), Constants.R_HAND);
//			Minion2.equipItem(Armory.Pencil(), Constants.R_HAND);
//			Boss.equipItem(Armory.UsedEraser(), Constants.L_HAND);
//			Minion1.equipItem(Armory.UsedEraser(), Constants.L_HAND);
//			Minion2.equipItem(Armory.UsedEraser(), Constants.L_HAND);
//			Boss.equipItem(Armory.StreetCloths(), Constants.BODY);
//			Boss.equipItem(Armory.DunceCap(), Constants.HEAD);
//			//Add three enemies to battle
//			BattleInfo.addBadCharacterToParty(Minion1, 11, 10);
//			BattleInfo.addBadCharacterToParty(Minion2, 11, 4);
//			BattleInfo.addBadCharacterToParty(Boss, 13, 7);
//			/////////////////////////
//			//finished battle setup//
//			/////////////////////////
//    		i = new Intent(this, Battle.class);
//    		GLView.renderer.clearScene();
//    		startActivity(i);
//    		break;
    	case com.Arkham.R.id.credits_button:
    		i = new Intent(this, Credits.class);
    		GLView.renderer.clearScene();
    		startActivity(i);
    		break;
    	case com.Arkham.R.id.btnTutorial:
    		i = new Intent(this, Tutorial.class);
    		GLView.renderer.clearScene();
    		startActivity(i);
    		break;
    	}
    }
    
    private void loadScene()
    {
    	/*
     	testingChar = new Actor(3,3,BitmapFactory.decodeResource(getResources(), R.drawable.testchar),64,64);
		testingChar.setTranslation(0.0f,-1.5f,-2.5f);
		
		testingChar.numXtile =6;
		GLView.renderer.addMesh(testingChar);
		 
      */
    	
    }
    
    protected void onResume() 
    {
    		        super.onResume();
    		        GLView.onResume();
    		        loadScene();
    }
     
    	    
    		    @Override
    protected void onPause()
    {
    	        super.onPause();
    	        GLView.onPause();
    	        
    	        
    }
   
    @Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return true;
	}

	//this method is called once the menu is selected
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.exit:
			this.finish();
			break;
		}
		return true;
	}

  
    private void createDemoCharacter(){
    	//set up Magus
		GameFile.party.characters[0].changeName("Magus");
		GameFile.party.characters[0].changeWorstFear("Homonyms");
		GameFile.party.inventory.gainItem(Armory.Pencil());
		GameFile.party.inventory.gainItem(Armory.Eraser());
		GameFile.party.inventory.gainItem(Armory.DunceCap());
		GameFile.party.inventory.gainItem(Armory.StreetCloths());
		GameFile.party.inventory.gainItem(Armory.Sneakers());
		GameFile.party.equip(0, Constants.R_HAND, GameFile.party.characters[0]);
		GameFile.party.equip(1, Constants.L_HAND, GameFile.party.characters[0]);
		GameFile.party.equip(2, Constants.HEAD, GameFile.party.characters[0]);
		GameFile.party.equip(3, Constants.BODY, GameFile.party.characters[0]);
		GameFile.party.equip(4, Constants.FEET, GameFile.party.characters[0]);
		//set up George
		GameFile.party.characters[1] = new Character();
		for(int l=0; l<4; l++){
		   GameFile.party.characters[1].gainALevel();
		}
		GameFile.party.characters[1].changeName("George");
		GameFile.party.characters[1].changeWorstFear("Homonyms");
		GameFile.party.inventory.gainItem(Armory.Pencil());
		GameFile.party.inventory.gainItem(Armory.Eraser());
		GameFile.party.inventory.gainItem(Armory.SchoolUniform());
		GameFile.party.inventory.gainItem(Armory.UniformShoes());
		GameFile.party.equip(5, Constants.R_HAND, GameFile.party.characters[1]);
		GameFile.party.equip(6, Constants.L_HAND, GameFile.party.characters[1]);
		GameFile.party.equip(7, Constants.BODY, GameFile.party.characters[1]);
		GameFile.party.equip(8, Constants.FEET, GameFile.party.characters[1]);		
		//set sprites
		Actor Sprite = new Actor(1,1, BitmapFactory.decodeResource(getResources(), com.Arkham.R.drawable.hero), 64,64);
		GameFile.party.characters[0].setActor(Sprite);
		GameFile.party.characters[0].setSpriteId(com.Arkham.R.drawable.hero);
		Sprite = new Actor(1,1, BitmapFactory.decodeResource(getResources(), com.Arkham.R.drawable.hero2), 64,64);
		GameFile.party.characters[1].setActor(Sprite);
		GameFile.party.characters[1].setSpriteId(com.Arkham.R.drawable.hero2);
    }
    
}